package getstudent

import (
	"database/sql"
	"fmt"

	"github.com/gin-gonic/gin"
	_ "github.com/lib/pq"
)

func Slogin(r *gin.Engine) {
	var err error
	//数据库连接
	var db *sql.DB
	db, err = sql.Open("postgres", "port=5432 user=postgres password=15566840887 dbname=mymbri sslmode=disable")
	var susername, suserpassword string    //定义两个字符
	r.GET("/login", func(c *gin.Context) { //登录页面
		// 获取参数
		susername = c.Query("username") //Query函数
		suserpassword = c.Query(("userpassword"))
		c.JSON(200, gin.H{
			"username":     susername,
			"userpassword": suserpassword,
		})

	})

	fmt.Printf("%s%s", susername, suserpassword)

	r.GET("/loginsuccess", func(c *gin.Context) { //注册页面
		var code, str, username, userpassword string
		code = "0"

		if err != nil { //用户名不能为空
			panic(err)
		}

		str = "SELECT userpassword FROM web where username='" + susername + "'"
		rows, err := db.Query(str) //上面的字符串代入数据库
		if err != nil {            //输入的密码不能为空
			panic(err)
		}
		for rows.Next() { //密码和确认密码不能为空
			err = rows.Scan(&userpassword)
			if err != nil {
				panic(err)
			}
		}
		if suserpassword == userpassword { //两次输入的密码是否一致，如果一致返回10
			code = "10"
		}
		fmt.Printf("web:%s%s", username, userpassword)
		// 获取参数
		c.JSON(200, gin.H{
			"code":      code,
			"str":       str,
			"spassword": suserpassword,
			"password":  userpassword,
		})
	})
}
